<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <!--测试用，上线复原-->
    <!--<meta name="format-detection" content="telephone=no" />-->
    <!--<meta name="apple-mobile-web-app-capable" content="yes" />-->
    <!--<meta name="apple-touch-fullscreen" content="yes">-->
    <!--<meta content="telephone=no,email=no" name="format-detection" />-->
    <meta name="flexible" content="initial-dpr=2,maximum-dpr=3" />
    <!--去缓存-->
    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0"><!--end-->
    <title>羊仓健康生活家</title>
    <link rel="stylesheet" href="css/style.css" />
    <link rel="stylesheet" href="css/common.css"/>
    <script type="text/javascript" src="js/jquery.min.js" ></script>
    <script type="text/javascript" src="js/basePathUtil.js" ></script>
    <!--货币格式化-->
    <script type="text/javascript" src="js/jquery-formatcurrency/jquery.formatCurrency.js" ></script>
    <!--弹出-->
    <script type="text/javascript" src="js/layer-mobile/layer.js" ></script>
    <!--省市区联动-->
    <script type="text/javascript" src="js/LAreaData2.js" ></script>
    <script type="text/javascript">
        var basePath=basePath();
        var access_token = GetUrl("access_token");
    </script>
</head>
<body class="bg_f2">
<div class="orderList orderDetail" id="tradeDetail">
    <!--<div class="order_time">下单时间：<span>2017-03-01  02:26:02</span><div class="del">取消订单</div></div>-->
    <!--<div class="item">-->
        <!--<div class="item_sup">-->
            <!--<div class="num">订单编号：0000</div>-->
            <!--&lt;!&ndash;<span>已发货</span>&ndash;&gt;-->
            <!--<span class="cancel">已取消</span>-->
            <!--&lt;!&ndash;<span class="finish">已完成</span>&ndash;&gt;-->
        <!--</div>-->
        <!--<ul>-->
            <!--<li>-->
                <!--<img src="img/img.png" alt=""/>-->
                <!--<div class="title">真硒天地天然营养富硒鸡蛋 年卡 30枚/月*12月</div>-->
                <!--<div class="price"><span>¥ 2376.00</span><em>×<i>1</i></em></div>-->
            <!--</li>-->
            <!--<li>-->
                <!--<img src="img/img.png" alt=""/>-->
                <!--<div class="title">真硒天地天然营养富硒鸡蛋 年卡 30枚/月*12月</div>-->
                <!--<div class="price"><span>¥ 2376.00</span><em>×<i>1</i></em></div>-->
            <!--</li>-->
        <!--</ul>-->
        <!--<div class="item_status">实付：0000.00 <span class="primty">付款</span></div>-->
        <!--&lt;!&ndash;<div class="item_status">实付：0000.00 <span class="finish">确认收货</span></div>&ndash;&gt;-->
    <!--</div>-->
    <!--<div class="address">-->
        <!--<div class="contact">-->
            <!--<div><span class="name">enidsal</span><span class="mobile">136****0000</span></div>-->
            <!--<p>北京市朝阳区樱花东街</p>-->
        <!--</div>-->
        <!--<ul>-->
            <!--<li><span>支付方式：</span>未选择</li>-->
            <!--<li><span>商品合作：</span>¥ 2523.00</li>-->
            <!--<li><span>运费：</span>¥ 0.00</li>-->
        <!--</ul>-->
        <!--<div class="payfor"><span>实付：</span>¥ 2523.00</div>-->
    <!--</div>-->
</div>
<script>
    function showAddress(trade) {
        var address = "";
        $.each(provs_data, function(index, content){
            if(content.value==trade.province){
                address+=content.text;
                $.each(citys_data[content.value], function(index, contentcity){
                    if(citys_data[content.value][index].value==trade.city){
                        address+=contentcity.text;
                        $.each( dists_data[contentcity.value], function(index, contentdists){
                            if(dists_data[contentcity.value][index].value==trade.county){
                                address+=contentdists.text;
                                return;
                            }
                        });
                        return;
                    }
                });
                return;
            }
        });
        return address;
    }
    /**
     * 显示支付方式
     * @param payStatus
     * @param payType
     * @returns {*}
     */
    function payType(payStatus,payType) {
        if(payStatus == null || payStatus == "" || payStatus == undefined)return;
        if(payStatus=="U"){return "待付款"}
        if(payStatus=="P"){
            switch(payType){
                case 0: return "支付宝";
                case 1: return "微信";
                case 2: return "微信";
                case 3: return "支付宝";
            }
        }
    }
    /**
     * 显示订单状态
     * @param status
     * @returns {*}
     */
    function showStatus(status) {
        if(status == null || status == "" || status == undefined)return;
        switch(status){
            case "S": return "待付款";
            case "D": return "待发货";
            case "R": return "已发货";
            case "E": return "已完成";
            case "C": return "已取消";
            case "F": return "已退款";
        }
    }
    /**
     * 更新订单状态
     * @param id
     * @param status
     */
    function updateTradeShow(id,status){
        if(id == undefined || id == null || id == '' || id<1){
            layer.open({content: '无法更新',skin: 'msg',time: 2});
            return;
        }
        if(status != "E" && status != "C" ){
            layer.open({content: '状态不匹配，无法更新',skin: 'msg',time: 2});
            return;
        }
        if(status == "C"){
            layer.open({
                content: '您确定要取消订单？'
                ,btn: ['我点错了', '确认取消']
                ,skin: 'footer'
                ,yes: function(index){
                    layer.close(index);
                },
                no:function (index) {
                    updateTrade(id,status);
                    layer.close(index);
                }
            });
            return ;
        }
        if(status == "E"){
            layer.open({
                content: '我已收到商品，确认收货？'
                ,btn: ['我点错了', '确认收货']
                ,skin: 'footer'
                ,yes: function(index){
                    layer.close(index);
                },
                no:function (index) {
                    updateTrade(id,status);
                    layer.close(index);
                }
            });
            return ;
        }
    }
    /**
     * 更新订单状态（取消订单、服务完成）
     * @param id
     */
    function updateTrade(id,status) {

        if(id == undefined || id == null || id == '' || id<1){
            layer.open({content: '无法更新订单',skin: 'msg',time: 2});
            return;
        }
        if(status != "E" && status != "C" ) {
            layer.open({content: '状态不匹配，无法更新', skin: 'msg', time: 2});
            return;
        }
        $.ajax({
            type:"POST",
            url:basePath+"/v1/trade/api/updateTradeStatus?access_token="+access_token,
            dataType : 'json',
            data:{
                tradeId:id,
                status:status
            },
            success:function(data){
                if(data.code==1000) {
                    switch (status){
                        case "E":
                            layer.open({content: "感谢您的购买",skin: 'msg',time: 2});
                            window.location.reload();
                            break;
                        case "C":
                            layer.open({content: "订单取消成功",skin: 'msg',time: 2});
                            window.location.reload();
                            break;
                    }
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    /**
     * 跳转继续支付
     * @param id
     */
    function pay(id) {
        window.location.href=basePath+"res/html/gwcfs.html?access_token="+access_token+'&tradeId='+id+"&t="+new Date().getTime();
    }
    /**
     * 显示按钮
     * @param id
     * @param status
     * @returns {*}
     */
    function showStatusBtn(id,status) {
        if(status == null || status == "" || status == undefined)return;
        switch(status){
            case "S": return '<span class="primty" onclick="pay('+id+')">付款</span>';
            case "R": return '<span class="primty" onclick="updateTradeShow('+id+',\'E\')">确认收货</span>';
            default: return "";
        }
    }
    function stripscript(s){
        var pattern = new RegExp("[`~!@#$%^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？]")
        var rs = "";
        for (var i = 0; i < s.length; i++) {
            rs = rs+s.substr(i, 1).replace(pattern, '');
        }
        return rs;
    }
    /**
     * 展示商品
     * @param trade
     * @param productList
     */
    function item(trade,productList){
        var item = '';
        item+='<div class="order_time">下单时间：<span>'+trade.createDate+'</span><div class="del" style="display: none;">取消订单</div></div>';
        item+='<div class="item">';
        item+='<div class="item_sup">';
        item+='<div class="num">订单编号：'+trade.tradeNo+'</div>';
        item+='<span class="cancel">'+showStatus(trade.status)+'</span>';
        item+='</div>';
        item+='<ul>';
        $.each(productList, function(index, content){
            item+='<li>';
            item+='<img src="'+productList[index].productUrl+'" alt=""/>';
            item+='<div class="title">'+productList[index].productName+'</div>';
            item+='<div class="price"><span>'+$.getFormattedCurrency(productList[index].unitPrice)+'</span><em>×<i>'+productList[index].quality+'</i></em></div>';
            item+='</li>';
        });
        item+='</ul>';
        item+='<div class="item_status">实付：'+$.getFormattedCurrency(trade.realPrice)+''+showStatusBtn(trade.id,trade.status)+'</div>';
        item+='</div>';
        item+='<div class="address">';
        item+='<div class="contact">';
        item+='<div><span class="name">'+trade.contactName+'</span><span class="mobile">'+trade.contactMobile+'</span></div>';
        item+='<p>'+stripscript(trade.contactAddress)+'</p>';
//        item+='<p>'+showAddress(trade)+''+trade.contactAddress+'</p>';
        item+='</div>';
        item+='<ul>';
        item+='<li><span>支付方式：</span>'+payType(trade.payStatus,trade.payType)+'</li>';
        item+='<li><span>商品总价：</span>'+$.getFormattedCurrency(trade.productTotalPrice)+'</li>';
        item+='<li><span>运费：</span>'+$.getFormattedCurrency(trade.freightPrice)+'</li>';
        item+='<li><span>订单总价：</span>'+$.getFormattedCurrency(trade.tradeTotalPrice)+'</li>';
        item+='</ul>';
        item+='<div class="payfor"><span>实付：</span>'+$.getFormattedCurrency(trade.realPrice)+'</div>';
        item+='</div>';
        $("#tradeDetail").append(item);
    }
    /**
     * 显示订单详细
     * @param id
     */
    function tradeDetailShow(id){
        $("#tradeDetail").html('');
        if(id=="" || id=="null" || id==null || id<1){
            layer.open({content: '订单不存在',skin: 'msg',time: 2});return;
        }
        $.ajax({
            type:"GET",
            url:basePath+"/v1/trade/api/showTrade?access_token="+access_token,
            dataType : 'json',
            data:{
                tradeId:id
            },
            success:function(data){
                if(data.code==1000) {
                    item(data.data.tradeSummary,data.data.tradeDetailsList);
                }else{
                    layer.open({content: data.msg,skin: 'msg',time: 2});
                }
            },
            error:function(data) {
            }
        });
    }
    tradeDetailShow(GetUrl("tradeId"));

</script>
</body>
</html>
